package controller

import (
	"bytes"
	"encoding/binary"
	"lnzw/engine/log"
	"lnzw/engine/net/tcp"

	"go.uber.org/zap"
	"google.golang.org/protobuf/proto"
)

type IController interface {
	ControllerId() int64
}

type DefaultController struct {
	Client *tcp.SocketClient
	Id     int64
}

func (c *DefaultController) ControllerId() int64 {
	return c.Id
}

func (c *DefaultController) SendBytes(code int, data []byte) {
	var buf bytes.Buffer
	bs := make([]byte, 4)
	binary.BigEndian.PutUint32(bs, uint32(code))
	buf.Write(bs)
	buf.Write(data)
	c.Client.SendMsg(buf.Bytes())
}

func (c *DefaultController) SendPb(code int, msg proto.Message) {
	bs, err := proto.Marshal(msg)
	c.SendBytes(code, bs)
	if err != nil {
		log.Logger.Error("SendPb error, marshal error", zap.Any("err", err))
	}
}
